<template>
  <div>
    <el-input
      v-model="ps_product_id"
      size="mini"
      placeholder="请输入网站的产品ID或者采集链接"
      class="input-with-select"
      clearable
      @keyup.native.enter="handleSearch"
    >
      <el-button
        slot="append"
        size="mini"
        icon="el-icon-search"
        type="primary"
        :loading="getloading"
        :disabled="loading"
        @click="handleSearch"
      >
        查询远程产品信息
      </el-button>
    </el-input>
    <el-button v-if="local_id ===''" size="mini" type="danger" @click="multiModifyPriceShown=true">
      批量改价
    </el-button>
    <div :style="showinfo">
      <el-tabs v-model="activeName" @tab-click="handleModifyTabClick">
        <el-tab-pane label="基本信息" name="first">
          <el-form ref="currentOpRow" :model="currentOpRow" label-width="100px">
            <el-form-item label="name">
              <el-input v-model="currentOpRow.name_modify" />
            </el-form-item>
            <el-form-item label="长描述">
              <tinymce ref="mt" v-model="currentOpRow.long_description_modify" :height="450" />
            </el-form-item>

            <el-form-item />
          </el-form>
        </el-tab-pane>
        <el-tab-pane v-if="this.ps_product_id !== ''" label="价格&变体" name="second5">
          <el-form ref="currentOpRow" :model="currentOpRow" label-position="left" label-width="120px">
            <el-form-item label="基本价格">
              <el-input v-model="currentOpRow.price_modify" />
            </el-form-item>

            <el-form-item label="统一价格">
              <el-switch v-model="currentOpRow.same_price" />
              <el-tag>为了保持feed价格统一，默认属性的价格需要跟基本价格一致</el-tag>
            </el-form-item>

            <el-form-item v-if="currentOpRow.fix_ps_attributes" label="未添加变体">
              <el-tag v-for="(item,index) in currentOpRow.fix_ps_attributes" :key="index" type="danger">{{ item }}
              </el-tag>
              <el-button type="text" size="mini" style="float: right;margin-left: 10px" @click="handleFixAttributes">
                修复变体组合
              </el-button>
            </el-form-item>
            <el-form-item v-if="!currentOpRow.same_price">
              <el-table border :data="currentOpRow.ps_attributes" style="width: 100%" height="500px">
                <el-table-column align="center" label="图片" prop="image">
                  <template slot-scope="scope">
                    <img
                      :src="scope.row.image"
                      class="image"
                      width="100px"
                      height="100px"
                    >
                  </template>
                </el-table-column>
                <el-table-column align="center" label="属性组合" prop="name" sortable="custom" />
                <el-table-column align="center" label="增长价格" prop="price" sortable="custom">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.price" size="mini" class="mt-input-price" />
                  </template>
                </el-table-column>
                <el-table-column align="center" label="默认属性" prop="default_on" sortable="custom" />
                <el-table-column align="center" label="id" prop="id_image" sortable="custom" />
                <el-table-column align="center" label="操作" sortable="custom">
                  <template slot-scope="scope">
                    <el-button type="danger" size="mini" @click="handleShowSetCombinationImage(scope.row)">图片
                    </el-button>
                    <el-button type="danger" size="mini" @click="handleDeleteAttribute(scope.row)">删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" style="float: right;margin-bottom: 10px" type="success" :loading="modify_loading" @click="handleModifyPriceOnly">提交修改价格</el-button>
              <!--<el-button @click=handleModifyPriceAllbyPercent(currentOpRow.attrs_price_modify) icon="el-icon-modify">比例修改</el-button>-->
            </el-form-item>
          </el-form>

        </el-tab-pane>

        <el-tab-pane label="图片信息" name="first2">

          <el-button size="mini" style="float: right; margin-bottom: 10px" type="success" :loading="modify_loading" @click="handleModifyImages">提交图片修改</el-button>
          <editorImage color="#1890ff" style="float: right; margin-bottom: 10px;margin-right: 10px" class="editor-upload-btn" @successCBK="imageSuccessCBK" />
          <el-button v-if="this.remove_bg_urls.length" size="mini" style="float: right; margin-bottom: 10px; margin-right: 10px;" type="warning" :loading="modify_loading" @click="handleMultiRemoveBg">批量去水印</el-button>
          <el-table border :data="currentOpRow.image_list_modify" style="width: 100%" height="600px" @selection-change="handleChangeImagelist">
            <el-table-column
              type="selection"
              width="55"
            />
            <el-table-column align="center" label="图片" prop="url" width="240">
              <template slot-scope="scope">
                <!--                <img :src="scope.row.new_url" class="image" width="100px"-->
                <!--                     height="100px"/>-->
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.new_url"
                  :preview-src-list="[scope.row.new_url]"
                />
              </template>
            </el-table-column>
            <el-table-column align="center" label="url" prop="url">
              <template slot-scope="scope">
                <span style="display: block;text-align: left">原图:{{ scope.row.url }}</span>
                <span v-if="scope.row.removed_url" style="display: block;text-align: left">去水印:{{ scope.row.removed_url }}</span>
                <span v-if="scope.row.url !== scope.row.new_url" style="display: block;text-align: left">新图:{{ scope.row.new_url }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="主图" prop="main" width="100">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.main"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="handleStatusChange(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column align="center" label="feed中" prop="in_feed" width="100">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.in_feed"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="handleInFeedChange(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column align="center" label="修改链接" prop="main" width="180">
              <template slot-scope="scope">
                <el-button type="text" @click="handleModifyImageUrl(scope.row)">修改</el-button>
                <el-button type="text" @click="handleShowRemoveBg(scope.row)">去水印</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="meta信息" name="second">
          <el-form ref="currentOpRow" :model="currentOpRow" label-width="80px">
            <el-form-item label="meta title">
              <el-input v-model="currentOpRow.meta_title_modify" />
            </el-form-item>

            <el-form-item label="meta description">
              <el-input v-model="currentOpRow.meta_description_modify" type="textarea" />
            </el-form-item>

            <el-form-item label="meta keywords">
              <el-input v-model="currentOpRow.meta_keywords_modify" type="textarea" />
            </el-form-item>
            <!--            <el-form-item label="disable">-->
            <!--              <el-switch v-model="currentOpRow.isDisable"></el-switch>-->
            <!--            </el-form-item>-->
            <el-form-item />
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="短描述" name="second2">
          <el-form ref="currentOpRow" :model="currentOpRow" label-width="80px">
            <el-form-item label="短描述">
              <el-input v-model="currentOpRow.description_modify" type="textarea" :rows="5" />
              <!--<tinymce ref="mteditdescription" :height="300" v-model="currentOpRow.description"></tinymce>-->
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane v-if="this.ps_product_id !== ''" label="属性" name="six">
          <el-collapse v-model="group_activeName" accordion>
            <el-collapse-item
              v-for="(item, index) in currentOpRow.attrs_groups"
              :key="index"
              :name="item.name"
              :title="item.name"
              :value="item.value"
            >
              <el-table
                border
                :data="item.attributes"
                style="width: 100%"
              >
                <el-table-column
                  label="name"
                  prop="name"
                />
                <el-table-column
                  label="value"
                  prop="value"
                />
                <el-table-column
                  align="right"
                >
                  <template slot-scope="scope">
                    <el-button
                      type="danger"
                      size="mini"
                      @click="handleEditAttributeName(scope.row)"
                    >修改名称</el-button>
                    <!--                        <el-button-->
                    <!--                          size="mini"-->
                    <!--                          type="danger">Delete</el-button>-->
                  </template>
                </el-table-column>
              </el-table>
              <el-button type="success" size="mini" style="float: right" :loading="AddAttributeLoading" @click="handleAddAttribute(item)">{{ item.name }}添加属性
              </el-button>
            </el-collapse-item>
          </el-collapse>
          <span style="margin-bottom: 10px" />
        </el-tab-pane>
        <el-tab-pane v-if="this.ps_product_id !== ''" label="其他修改" name="eight">
          <el-form ref="form-eight" :inline="true" :model="currentOpRow" label-width="80px">
            <el-form-item label="重量(KG)">
              <el-input v-model="modify_weight" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="modify_weight_loading" @click="handleGetWeight(currentOpRow)">查询
              </el-button>
              <el-button type="danger" @click="handleModifyWeight(currentOpRow)">修改</el-button>
            </el-form-item>
          </el-form>
          <el-form ref="form-label1" :inline="true" :model="currentOpRow" label-width="180px">
            <el-form-item label="自动定义标签1(GMC feed)">
              <el-input v-model="currentOpRow.customLabel1" />
            </el-form-item>
            <el-form-item>
              <el-button type="danger" @click="handleModifyPrestashopWkField(currentOpRow)">修改</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

      </el-tabs>
    </div>
    <hr>
    <span slot="footer" class="dialog-footer" :style="showinfo" style="margin-top: 10px;float: right">

      <el-radio-group v-if="this.ps_product_id !== ''" v-model="modify_combination">
        <!--                  <el-radio label="1">重新上传产品</el-radio>-->
        <el-radio label="0">修改产品</el-radio>
      </el-radio-group>
      <el-button
        v-if="this.ps_product_id !== ''"
        type="danger"
        :loading="modify_loading"
        @click="handleSaveModifyRemote(true)"
      >提交所有修改</el-button>

      <el-button
        v-if="this.ps_product_id !== ''"
        type="danger"
        :loading="delete_loading"
        style="float: left;padding: 5px 5px 5px;"
        @click="handleDeleteRemoteProduct(true)"
      >删除产品</el-button>
      <a
        target="_blank"
        :href="currentOpRow.target_url !== ''? currentOpRow.target_url: currentOpRow.url"
        style="float: left;padding: 5px 5px 5px;"
      >打开产品</a>
      <a target="_blank" :href="clean_cache_url" style="float: left;margin-left: 5px;padding: 5px 5px 5px;">清除缓存</a>
      <a target="_blank" :href="currentOpRow.url" style="float: left;margin-left: 5px;padding: 5px 5px 5px;">采购</a>

    </span>
    <modifymultiprice
      :domain="domain"
      :pst-visible="multiModifyPriceShown"
      @closeMyDialog="multiModifyPriceShown=false"
    />

    <el-dialog v-loading="dialogSetCombinationLoading" append-to-body :title="SetCombinationImageTitle" :visible.sync="dialogSetCombinationImageVisible">
      <el-table border :data="currentOpRow.image_list_modify" style="width: 100%">
        <el-table-column align="center" label="图片" prop="url" sortable="custom">
          <template slot-scope="scope">
            <img
              :src="scope.row.new_url"
              class="image"
              width="50px"
              height="50px"
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="id" prop="id_image" sortable="custom" />
        <el-table-column align="center" label="修改链接" prop="main" sortable="custom">
          <template slot-scope="scope">
            <el-button v-if="currentCombinationImage !== scope.row.id_image" type="text" @click="handleSetCombinationImage(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogSetCombinationImageVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <el-dialog append-to-body :title="process_result.task" :visible.sync="showProcessResult">
      <h3 v-if="process_result.success">执行结果</h3>
      <h3 v-else style="color: red">执行结果</h3>
      <div>
        <pre>{{ process_result.content }}</pre>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showProcessResult = false">关 闭</el-button>
      </span>
    </el-dialog>
    <remove-bg
      :is-visible="removeBgVisible"
      :url="remove_bg_url"
      :urls="remove_bg_urls"
      :product_id="remove_bg_product_id"
      :sync="remove_bg_sync"
      @close="removeBgVisible=false"
      @success="removeBgSuccess"
    />
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import editorImage from '../../../components/Tinymce/components/EditorImage'
import {
  crawl_modify_remote, crawl_update_directly_upload_remote,
  delete_remote_product
} from '@/api/erp/crawlproduct'

import { SetMainImage } from '@/api/erp/crawlproductimage'
import {
  GetPsProduct,
  FixAttributes,
  DeleteAttribute,
  ModifyPsProductWeight,
  modify_ps_wk_product_field,
  GetPsProductWeight,
  SetCombinationImage,
  ModifyPsAttributeName
} from '@/api/prestashop/prestashopapi'
// import Feedproduct from "@/views/orders/element/feedproduct"
import modifymultiprice from '@/views/prestashop/element/modifymultiprice'
import { AddPsAttrubute } from '../../../api/prestashop/prestashopapi'
import RemoveBg from './remove-bg'

export default {
  name: 'PrestaproductInner',
  components: { RemoveBg, modifymultiprice, Tinymce, editorImage },
  props: ['domain', 'product_id', 'shown'],
  data() {
    return {
      process_result: {
        success: false,
        task: '',
        content: ''
      },
      removeBgVisible: false,
      remove_bg_url: '',
      remove_bg_urls: [],
      remove_bg_product_id: '',
      remove_bg_sync: true, // 是否立即返回
      showProcessResult: false,
      delete_loading: false,
      modify_loading: false,
      modify_type: 'all', // 用于只修改图片，或者价格的功能
      local_id: '',
      modify_weight: 0,
      ps_website_id: this.website_id,
      modify_weight_loading: false,
      orderdetailVisual: false,
      multiModifyPriceShown: false,
      title: '修改产品',
      activeName: 'first',
      getloading: false,
      dialogVisible: false,
      loading: false,
      url: '',
      ps_product_id: '',
      showinfo: 'display:none;',
      searchResults: [],
      searchImageUrl: '',
      upload_task_id: '',
      update_direct_remote_loading: false,
      temp_all_price: 0,
      useModifyPrice: 0,
      modify_combination: '0',
      clean_cache_url: '',
      group_activeName: '',
      SetCombinationImageTitle: '指定组合图片',
      combination: '',
      AddAttributeLoading: false,
      currentCombinationImage: '',
      currentCombinationid_product_attribute: '',
      dialogSetCombinationImageVisible: false,
      dialogSetCombinationLoading: false,
      uploaded_image_urls: [],
      currentOpRow: {
        'id': 0,
        'same_price': true,
        'name': '',
        'price': '',
        'description': '',
        'long_description_modify': '',
        'meta_title': '',
        'meta_description': '',
        'meta_keywords': '',
        'image_list': [],
        'upload_tasks': [],
        'fix_ps_attributes': [],
        'attrs_price': []
      }
    }
  },
  watch: {
    domain: {
      handler(newName, oldName) {
        if (newName !== oldName) {
          this.resetCurrentRow()
        }
        console.log('prestaproduct_inner get new domain', newName)
      },
      immediate: true,
      deep: true
    },
    product_id: {
      handler(newName, oldName) {
        if (newName) {
          this.ps_product_id = newName
          if (oldName !== newName) {
            this.handleSearch()
          }
        }
      },
      immediate: true,
      deep: true
    },
    shown(v) {
      if (v) {
        this.$nextTick(() => {
          // 重新初始化
          this.$refs.mt.destroyTinymce()
          this.$refs.mt.initTinymce()
          console.log('init mce')
          if (this.$refs.mt.hasInit) {
            console.log('set long description')
            this.$refs.mt.setContent(this.currentOpRow.long_description_modify != null ? this.currentOpRow.long_description_modify : '')
          }
          // if (this.$refs.mteditdescription.hasInit && this.currentOpRow.description != null) {
          //   this.$refs.mteditdescription.setContent(this.currentOpRow.description != null ? this.currentOpRow.description : '')
          // }
        })
      }
    }
  },
  methods: {
    handleChangeImagelist(val) {
      if (val) {
        this.remove_bg_urls = val.map(item => { return item.url })
      } else {
        this.remove_bg_urls = []
      }
    },
    handleMultiRemoveBg() {
      this.removeBgVisible = true
    },
    handleShowRemoveBg(row) {
      console.log(row)
      this.remove_bg_url = row.url
      this.removeBgVisible = true
    },
    removeBgSuccess(val) {
      if (val.length) {
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < this.currentOpRow.image_list_modify.length; j++) {
            if (this.currentOpRow.image_list_modify[j]['url'] === val[i]['url']) {
              this.currentOpRow.image_list_modify[j]['new_url'] = val[i]['removed_url']
            }
          }
        }
        this.$message.success('去水印成,共有' + val.length + '图片')
      } else {
        this.$message.error('去水印失败，没有返回结果')
      }
    },
    resetProcessResult() {
      this.process_result = {
        success: false,
        task: '',
        content: ''
      }
    },
    imageSuccessCBK(arr) {
      const _this = this
      arr.forEach(v => {
        _this.uploaded_image_urls.push(v.url)
      })
    },
    resetCurrentRow() {
      this.currentOpRow = {
        'id': 0,
        'same_price': true,
        'name': '',
        'price': '',
        'description': '',
        'long_description_modify': '',
        'meta_title': '',
        'meta_description': '',
        'meta_keywords': '',
        'image_list': [],
        'upload_tasks': [],
        'fix_ps_attributes': [],
        'attrs_price': []
      }
      this.$nextTick(() => {
        // 重新初始化
        this.$refs.mt.destroyTinymce()
        this.$refs.mt.initTinymce()
        console.log('init mce')
        if (this.$refs.mt.hasInit) {
          console.log('set long description')
          this.$refs.mt.setContent(this.currentOpRow.long_description_modify != null ? this.currentOpRow.long_description_modify : '')
        }
        // if (this.$refs.mteditdescription.hasInit && this.currentOpRow.description != null) {
        //   this.$refs.mteditdescription.setContent(this.currentOpRow.description != null ? this.currentOpRow.description : '')
        // }
      })
    },
    handleGetWeight(row) {
      this.modify_weight_loading = true
      GetPsProductWeight({
        domain: this.domain,
        id_product: this.ps_product_id

      }
      ).then(response => {
        this.modify_weight_loading = false
        this.modify_weight = response.data.weight
      })
    },
    handleModifyPrestashopWkField(row) {
      this.loading = true
      modify_ps_wk_product_field({
        domain: this.domain,
        customLabel1: row.customLabel1,
        id_product: this.ps_product_id

      }
      ).then(response => {
        this.loading = false
        this.$notify({
          title: '成功',
          message: '',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleModifyWeight(row) {
      this.loading = true
      ModifyPsProductWeight({
        weight: this.modify_weight,
        domain: this.domain,
        customLabel1: row.customLabel1,
        id_product: this.ps_product_id

      }
      ).then(response => {
        this.loading = false
        this.$notify({
          title: '成功',
          message: '',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleEditAttributeName(row) {
      this.$prompt('请注意，更改名称可能会影响多个使用此属性的产品', '修改属性名称', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        ModifyPsAttributeName({
          name: value,
          domain: this.domain,
          id_attribute: row.value
        }
        ).then(response => {
          this.loading = false
          this.$notify({
            title: '修改名称成功，清空缓存生效',
            message: '',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    handleAddAttribute(row) {
      this.$prompt('请输入名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.AddAttributeLoading = true
        AddPsAttrubute({
          name: value,
          domain: this.domain,
          id_product: this.ps_product_id,
          id_attribute_group: row.value,
          position: row.attributes.length
        }
        ).then(response => {
          this.AddAttributeLoading = false
          this.$notify({
            title: '修改名称成功，刷新缓存生效',
            message: '',
            type: 'success',
            duration: 2000
          })
          this.handleSearch()
        })
      }).catch(() => {
        this.AddAttributeLoading = false
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    handleModifyPriceAll(row) {
      this.$prompt('请输入价格', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        row.forEach(item => {
          this.currentOpRow.price_modify = value
          item.skuVal.actSkuMultiCurrencyCalPrice = value
          item.skuVal.actSkuCalPrice = value
          item.skuVal.skuMultiCurrencyCalPrice = value
          item.skuVal.skuMultiCurrencyDisplayPrice = value
          item.skuVal.actSkuMultiCurrencyDisplayPrice = value
          item.skuVal.skuCalPrice = value
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    handleModifyPriceAllbyPercent(row) {
      this.$prompt('请输入价格比例', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        row.forEach(item => {
          item.skuVal.actSkuMultiCurrencyCalPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
          item.skuVal.actSkuCalPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
          item.skuVal.skuMultiCurrencyCalPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
          item.skuVal.skuMultiCurrencyDisplayPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
          item.skuVal.actSkuMultiCurrencyDisplayPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
          item.skuVal.skuCalPrice = (value * parseFloat(item.skuVal.actSkuMultiCurrencyCalPrice)).toFixed(2)
        })
        this.currentOpRow.price_modify = (value * parseFloat(this.currentOpRow.price_modify)).toFixed(2)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    handleModifyPrice(row) {
      this.currentOpRow.price_modify = row.actSkuMultiCurrencyCalPrice
      row.actSkuCalPrice = row.actSkuMultiCurrencyCalPrice
      row.skuMultiCurrencyCalPrice = row.actSkuMultiCurrencyCalPrice
      row.skuMultiCurrencyDisplayPrice = row.actSkuMultiCurrencyCalPrice
      row.actSkuMultiCurrencyDisplayPrice = row.actSkuMultiCurrencyCalPrice
      row.skuCalPrice = row.actSkuMultiCurrencyCalPrice
    },
    handleModifyImageUrl(row) {
      this.$prompt('请输入图片链接', '原图链接:' + row.url, {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        row.new_url = value
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    handleInFeedChange(row) {
      let total_in_feed = 0
      console.log('infeed', row.in_feed)
      for (let i = 0; i < this.currentOpRow.image_list_modify.length; i++) {
        if (this.currentOpRow.image_list_modify[i].in_feed === true) {
          total_in_feed += 1
        }
      }
      if (total_in_feed <= 0) {
        alert('您必须选择至少1张图片在feed中')
        row.in_feed = true
      }
      console.log('total_in_feed', total_in_feed)
    },
    handleStatusChange(row) {
      if (this.ps_product_id === '') {
        this.loading = true
        SetMainImage({
          image_id: row.id,
          source: row.is_local ? 'local' : 'crawl',
          product_id: this.product_id !== '' ? this.product_id : this.local_id

        }
        ).then(response => {
          this.loading = false
          this.currentOpRow.image_list_modify.forEach(item => {
            if (item.id_image !== row.id_image) {
              item.main = !row.main
            }
          })
          this.$notify({
            title: '成功',
            message: '',
            type: 'success',
            duration: 2000
          })
        })
      } else {
        this.currentOpRow.image_list_modify.forEach(item => {
          if (item.id_image !== row.id_image) {
            item.main = !row.main
          }
        })
      }
    },
    handleUpdateDirectupload() {
      this.update_direct_remote_loading = true
      crawl_update_directly_upload_remote(JSON.stringify({
        useModifyPrice: this.useModifyPrice,
        id_task: this.upload_task_id
      }))
        .then(response => {
          this.$notify({
            title: '修改成功',
            message: '',
            type: 'success',
            duration: 2000
          })
          this.update_direct_remote_loading = false
        }).catch(err => {
          this.$notify({
            title: '失败',
            message: err,
            type: 'success',
            duration: 2000
          })
          this.update_direct_remote_loading = false
        })
    },
    handleShowSetCombinationImage(combination) {
      this.combination = combination
      this.SetCombinationImageTitle = '指定' + combination.name + '图片'
      this.currentCombinationImage = combination.id_image
      this.currentCombinationid_product_attribute = combination.id_product_attribute
      this.dialogSetCombinationImageVisible = true
    },
    handleSetCombinationImage(row) {
      this.dialogSetCombinationLoading = true
      SetCombinationImage({
        id_product_attribute: this.currentCombinationid_product_attribute,
        domain: this.domain,
        id_image: row.id_image
      }
      ).then(response => {
        this.combination.id_image = row.id_image
        this.dialogSetCombinationLoading = false
        this.dialogSetCombinationImageVisible = false
        this.$notify({
          title: '修改图片成功',
          message: '',
          type: 'success',
          duration: 2000
        })
      }).catch(() => {
        this.dialogSetCombinationLoading = false
        this.$message({
          type: 'info',
          message: '失败'
        })
      })
    },
    handleDeleteAttribute(row) {
      this.$confirm('真的删除变体组合' + row.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        DeleteAttribute({
          id_product: parseInt(this.ps_product_id),
          id_product_attribute: row.id_product_attribute,
          website_id: parseInt(this.currentOpRow.website_id)
        })
          .then(response => {
            this.$notify({
              title: '修复成功,请到网站检查',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            // 移除
            this.currentOpRow.ps_attributes = this.currentOpRow.ps_attributes.filter(function(item) {
              return item.id_product_attribute !== row.id_product_attribute
            })
            this.loading = false
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: err,
              type: 'success',
              duration: 2000
            })
            this.loading = false
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleNameChange(value) {
      this.currentOpRow.name_modify = value
      console.log(this.currentOpRow.name_modify)
    },
    handleFixAttributes() {
      this.$confirm('此操作会增加已存在的所有变体组合,将不可恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        FixAttributes({
          id_product: parseInt(this.ps_product_id),
          website_id: parseInt(this.currentOpRow.website_id)
        })
          .then(response => {
            this.$notify({
              title: '修复成功,请到网站检查',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: err,
              type: 'success',
              duration: 2000
            })
            this.loading = false
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleModifyImages() {
      this.modify_type = 'image'
      this.handleSaveModifyRemote(true)
    },
    handleModifyPriceOnly() {
      this.modify_type = 'price'
      this.handleSaveModifyRemote(true)
    },
    handleDeleteRemoteProduct() {
      this.$prompt('请输入删除原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.delete_loading = true
        delete_remote_product({
          ps_product_id: this.ps_product_id,
          url: this.currentOpRow.target_url !== '' ? this.currentOpRow.target_url : this.currentOpRow.url,
          images: this.currentOpRow.image_list,
          reason: value,
          website_id: this.currentOpRow.website_id
        })
          .then(response => {
            this.$notify({
              title: '成功',
              message: response.message,
              type: 'success',
              duration: 2000
            })
            this.delete_loading = false
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: err,
              type: 'success',
              duration: 2000
            })
            this.delete_loading = false
          })
      }).catch(() => {
      })
    },
    handleSaveModifyRemote(isRemote) {
      this.modify_loading = true
      this.resetProcessResult()
      crawl_modify_remote(JSON.stringify({
        data: this.currentOpRow,
        isRemote: isRemote,
        modify_type: this.modify_type,
        modify_combination: this.modify_combination,
        domain: this.domain
      }))
        .then(response => {
          // this.$notify({
          //   title: '成功',
          //   message: response.msg,
          //   type: 'success',
          //   duration: 10000
          // })
          this.modify_loading = false
          this.process_result.success = response.success
          this.process_result.content = response.msg
          this.process_result.task = '修改结果'
          this.showProcessResult = true
        }).catch(err => {
          this.$notify({
            title: '失败',
            message: err,
            type: 'success',
            duration: 2000
          })
          this.modify_loading = false
          this.process_result.success = false
          this.process_result.content = err
          this.process_result.task = '修改图片'
          this.showProcessResult = true
          this.modify_loading = false
        })
    },
    handleModifyLocalProduct() {
      this.ps_product_id = ''
      this.local_id = this.currentOpRow.local_id
      this.url = this.currentOpRow.url
      this.showinfo = 'display:none;'
    },
    handleModifyTabClick() {
      console.log('tab click')
    },
    handleSearch() {
      if (this.ps_product_id === '' && this.local_id === '') {
        return
      }
      this.resetCurrentRow()
      this.showinfo = 'display:none;'
      this.getloading = true
      GetPsProduct({
        'domain': this.domain,
        'ps_product_id': this.ps_product_id,
        'local_id': this.local_id,
        'url': this.url
      }).then(respone => {
        this.currentOpRow = respone.data
        if (typeof (this.ps_product_id) !== 'number' && this.ps_product_id !== '') {
          this.ps_product_id = this.currentOpRow.ps_product
        }
        this.currentOpRow.same_price = true
        this.currentOpRow.ps_attributes.forEach(item => {
          if (parseFloat(item.price) !== 0) {
            this.currentOpRow.same_price = false
          }
        })
        this.clean_cache_url = respone.clean_cache_url
        this.getloading = false
        this.showinfo = 'display:inline;'
        this.$nextTick(() => {
          if (this.$refs.mt.hasInit) {
            console.log('set long description')
            this.$refs.mt.setContent(this.currentOpRow.long_description_modify != null ? this.currentOpRow.long_description_modify : '')
          }
          // if (this.$refs.mteditdescription.hasInit && this.currentOpRow.description != null) {
          //   this.$refs.mteditdescription.setContent(this.currentOpRow.description != null ? this.currentOpRow.description : '')
          // }
        })
      }).catch(e => {
        this.getloading = false
      })
    },
    close() {
      // this.ps_product_id = ''
      // this.local_id = ''
      // this.showinfo = "display:none;"
      this.$emit('closePstDialog', false)
    }
  }
}
</script>

<style scoped>

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }
</style>
